#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@创建时间    : 2022/10/20  14:47
@作者  : st
@文件名: data_process.py
@项目名: PyCharm
@文件描述:医学会数据转换导入标注工具
    
"""
import json
import random
import re

from utils.pkuseg_utils import PkusegUtils
pkuseg = PkusegUtils()

pat_num = r'^\d+\.$'
all_datas = []
temp_data = []
for l in open('../data/matadata/1020/Sino分类检基础医学10745_中华医学会_2022.1.1-2022.10.20.txt', 'r', encoding='utf-8'):
    l = l.strip().replace('\n', '')
    if re.findall(pat_num, l):
        print(l)
        if temp_data:
            all_datas.append('\n'.join(temp_data))
            temp_data = []
        continue
    if l.startswith('【标题】') or l.startswith('【摘要】'):
        temp_data.append('\t'.join(pkuseg.split_pukseg(l)))
if temp_data:
    all_datas.append('\n'.join(temp_data))


# path = '../data/matadata/1020/Sino分类检基础医学10745.csv'
# df = pd.DataFrame(data=all_datas)
# df.to_csv(path, header=False, index=False, mode='a')


def text_spit(text_list, split_str, split_type):
    """
    文本实体位置替换
    :param text_list:
    :param split_str:
    :param split_type:
    :return:
    """
    def text_zy(text):
        text = text.replace('(', '\\(').replace(')', '\\)').replace('+', '\\+')
        return text
    temp_list = []
    for text in text_list:
        if not isinstance(text, str):
            temp_list.append(text)
            continue
        if split_str not in text:
            temp_list.append(text)
            continue
        texts = re.split('(' + text_zy(split_str)+ ')', text)
        for t in texts:
            if not t:
                continue
            if t == split_str:
                temp_list.append((split_str, split_type))
                continue
            temp_list.append(t)
    return temp_list


def create_json_data():
    with open('../data/matadata/1020/all标准映射表.json', 'r', encoding='utf-8') as f:
        medical_dict = json.load(f)

    standards = []
    for tag in medical_dict.keys():
        temp_stantd = [(x, tag) for x in medical_dict[tag].keys()]
        standards.extend(temp_stantd)
    standards = sorted(standards, key=lambda x:len(x[-1]), reverse=True)
    def list_ext(list, text):
        result = []
        temp_str = ''
        for lt in list:
            # 如果格式为‘str’
            if isinstance(lt, str):
                temp_str += lt
            else:
                value, tag = lt
                index_start = len(temp_str)
                temp_str += value
                index_end = len(temp_str)
                value_dict = {'start': index_start, 'end': index_end, 'text': value, 'labels': [tag]}
                res = {"value": value_dict, "from_name": "label", "to_name": "text", "type": "labels"}
                result.append(res)
        data_json = {"annotations": [{"result": result}], "data": {"text": text}}
        return data_json

    all_data = []
    # random.shuffle(all_datas)
    step = len(all_datas)/6
    num = 1
    for i, text in enumerate(all_datas):
        if i >= step*num:
            with open('../data/matadata/1020/Sino分类检基础医学_' + str(num) + '.json', "w", encoding='utf-8') as f:
                f.truncate()
                json.dump(all_data, f, ensure_ascii=False)
            print('====', num)
            num += 1
            all_data = []
        text = text.strip()
        temp_list = [text]
        # 对每句遍历所有的实体进行替换
        # for word, tag in standards:
        #     if word in text:
        #         temp_list = text_spit(temp_list, word, tag)
        res = list_ext(temp_list, text)
        all_data.append(res)
    if all_data:
        with open('../data/matadata/1020/Sino分类检基础医学_' + str(num) + '.json', "w", encoding='utf-8') as f:
            f.truncate()
            json.dump(all_data, f, ensure_ascii=False)
        print('====', num)
    print('Ok')


if __name__ == '__main__':
    create_json_data()